home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2001 December / pcwk12201b.iso / Wersje pelne i specjalne / Winamp 2.77 i 3.0beta / wasabi-sdk_beta1.exe / studio / common / node.h < prev    next >
C/C++ Source or Header  |  2001-10-08  |  4KB  |  147 lines

  1. /*
  2.  
  3.   Nullsoft WASABI Source File License
  4.  
  5.   Copyright 1999-2001 Nullsoft, Inc.
  6.  
  7.     This software is provided 'as-is', without any express or implied
  8.     warranty.  In no event will the authors be held liable for any damages
  9.     arising from the use of this software.
  10.  
  11.     Permission is granted to anyone to use this software for any purpose,
  12.     including commercial applications, and to alter it and redistribute it
  13.     freely, subject to the following restrictions:
  14.  
  15.     1. The origin of this software must not be misrepresented; you must not
  16.        claim that you wrote the original software. If you use this software
  17.        in a product, an acknowledgment in the product documentation would be
  18.        appreciated but is not required.
  19.     2. Altered source versions must be plainly marked as such, and must not be
  20.        misrepresented as being the original software.
  21.     3. This notice may not be removed or altered from any source distribution.
  22.  
  23.  
  24.   Brennan Underwood
  25.   brennan@nullsoft.com
  26.  
  27. */
  28.  
  29. #ifndef _NODE_H
  30. #define _NODE_H
  31.  
  32. #include "../common/ptrlist.h"
  33.  
  34. //
  35. //        node.h
  36. //
  37. //    Some node classes.
  38. //
  39.  
  40.  
  41.  
  42. // ==========================================================================
  43. //
  44. //        class Node
  45. //
  46. //    A generic "node" object from which one may subclass and create a set of
  47. //    disparate objects that may be assembled into a hierarchical tree.
  48. //
  49. class Node {
  50. private:
  51.     PtrList<Node>                                  childlist;
  52.     Node *                                                parent;
  53.  
  54. protected:
  55.  
  56. #ifdef  _DEBUG
  57.     static int                                        count;
  58. #endif//_DEBUG
  59.  
  60. public:
  61.  
  62.     Node(Node * myParent = NULL) : childlist(), parent(myParent) { 
  63. #ifdef  _DEBUG
  64.         count++; 
  65. #endif//_DEBUG
  66.     }
  67.  
  68.     Node(const Node & a) : childlist(a.childlist), parent(a.parent) { 
  69. #ifdef  _DEBUG
  70.         count++; 
  71. #endif//_DEBUG
  72.     }
  73.  
  74.     virtual ~Node() { 
  75. #ifdef  _DEBUG
  76.         count--; 
  77. #endif//_DEBUG
  78.     };
  79.  
  80.     int nodeCount() {
  81. #ifdef  _DEBUG
  82.         return count;
  83. #else //_DEBUG
  84.         return -1;
  85. #endif//_DEBUG
  86.     }
  87.  
  88.     //
  89.     //    Const Data Accessor Methods
  90.     int getNumChildren() const { 
  91.         return childlist.getNumItems(); 
  92.     }
  93.  
  94.     Node * enumChild(int index) const { 
  95.         return childlist[index];
  96.     }
  97.  
  98.     Node * getParent() const {
  99.         return parent;
  100.     }
  101.  
  102.     //
  103.     //    Nonconst Data Manipulator Methods
  104.     Node * addChild(Node * child) {
  105.         child->setParent(this);
  106.         return childlist.addItem(child);
  107.     }
  108.  
  109.     Node * setParent(Node * myParent) {
  110.         return parent = myParent;
  111.     }
  112.  
  113.     PtrList< Node >    & ChildList() {
  114.         return childlist;
  115.     }
  116. };
  117.  
  118. // ==========================================================================
  119. //
  120. //        class NodeC< TPayload >
  121. //
  122. //    If you would rather use Node as a container class than as a base class, 
  123. //    this object will allow you to contain a payload instance and be accessed
  124. //    as if it were the payload instance using a reference operator and an
  125. //    implicit cast operator.
  126. //
  127. template < class TPayload >
  128. class NodeC : public Node {
  129. protected:
  130.     TPayload                                payload;
  131. public:
  132.     NodeC( const TPayload & myPayload, NodeC * myParent = NULL ) : Node( myParent ), payload( myPayload ) {}
  133.     NodeC( const NodeC & a ) : Node( a ), payload( a.payload ) {}
  134.  
  135.     //
  136.     // In addition to being able to call all of the Node methods, you can also
  137.     // simply treat the node objects themselves as if they were the payload 
  138.     // instantiation through these simple methods:
  139.  
  140.     // Explicit reference operator - for lvalues and force rvalues.
  141.     TPayload & operator () ( void ) { return payload; }
  142.     // Implicit cast operator - for rvalues that can accept a TPayload &
  143.     operator TPayload &  ( void ) { return payload; }
  144. };
  145.  
  146. #endif//_NODE_H
  147.